package com.zone.gateway.core.socket.agreement.impl;

import com.zone.gateway.core.socket.agreement.IRequestParser;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.QueryStringDecoder;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: zongzi
 * @description: get请求参数解析器
 * @date: 2025/4/22
 */
public class GetParser implements IRequestParser {

    @Override
    public Map<String, Object> analysis(FullHttpRequest httpRequest) {
        Map<String, Object> resultMap = new HashMap<>();
        // 获取请求的 URI
        String uri = httpRequest.uri();
        // 使用 QueryStringDecoder 解析查询参数
        QueryStringDecoder decoder = new QueryStringDecoder(uri);

        // 获取解析后的参数http//xxx?name=zs&age=12
        Map<String, List<String>> parameters = decoder.parameters();
        for (Map.Entry<String, List<String>> entry : parameters.entrySet()) {
            String key = entry.getKey();
            List<String> paramsValues = entry.getValue();
            resultMap.put(key, paramsValues.get(0));
        }
        return resultMap;
    }
}
